package com.wjy.Data_Structure.linearlist.listslinkimpl;

import com.wjy.Data_Structure.linearlist.common.Iterator;
import com.wjy.Data_Structure.linearlist.common.LinkedList;
import com.wjy.Data_Structure.linearlist.common.Node;
import com.wjy.Data_Structure.linearlist.exception.OutOfBoundaryException;

public class LinkedListIterator implements Iterator {
	private LinkedList list;// 链接表
	private Node current;// 当前结点

	public LinkedListIterator(LinkedList list) {
		this.list = list;
		if (list.isEmpty())
			current = null;
		else
			current = list.first();// 从第一个元素开始
	}

	@Override
	public void first() {
		if (list.isEmpty())
			current = null;
		else
			current = list.first();// 从第一个元素开始

	}

	@Override
	public void next() throws OutOfBoundaryException {
		if (isDone())
			throw new OutOfBoundaryException("错误：已经没有元素");
		if (current == list.last())
			current = null;// 当前元素后面没有更多元素
		else
			current = list.getNext(current);
	}

	@Override
	public boolean isDone() {
		return current == null;
	}

	@Override
	public Object currentItem() {
		if (isDone())
			throw new OutOfBoundaryException("错误：已经没有元素");
		return current.getData();
	}

}
